package cn.controller;

import cn.config.AlipayConfig;
import cn.domain.Oder;
import cn.service.OderService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class XxlController {
    @Autowired
    OderService oderService;
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    RedisTemplate redisTemplate;
@XxlJob(value = "paopi")
    public  void paopi(){
    Oder oder = new Oder();
    oder.setMsg("正在支付");
    LambdaQueryWrapper<Oder> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Oder::getMsg,oder.getMsg());
    List<Oder> list = oderService.list(wrapper);

    for (Oder oder1 : list) {
 
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(oder1.getEmail());

        message.setTo(oder1.getEmail());
        message.setText("请及时支付");
        message.setSubject("催费订单");
        javaMailSender.send(message);
        Long increment = redisTemplate.opsForValue().increment("次数："+oder1.getOderNo(), 1);
                System.out.println(increment);
        if (increment>=3){
            oderService.update(Wrappers.lambdaUpdate(Oder.class).set(Oder::getStatus,2).set(Oder::getMsg,"未及时支付订单已取消").eq(Oder::getOderNo,oder1.getOderNo()));


            System.err.println("未及时支付订单已取消");

        }
    }
   

}
    @XxlJob(value = "duizhang")
    public  void duizhang() throws AlipayApiException {
        Oder oder = new Oder();
        oder.setMsg("正在支付");
        LambdaQueryWrapper<Oder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Oder::getMsg,oder.getMsg());
        List<Oder> list = oderService.list(wrapper);
        for (Oder oder1 : list) {
            
  
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = oder1.getOderNo();
        //支付宝交易号
        String trade_no = oder1.getApplyNo();
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+"\"trade_no\":\""+ trade_no +"\"}");

        //请求
            AlipayTradeQueryResponse execute = alipayClient.execute(alipayRequest);
            String code = execute.getCode();
            String msg = execute.getMsg();
            String subMsg = execute.getSubMsg();
            System.err.println(code);
            System.err.println(msg);
            System.err.println(subMsg);
            if ("40004".equals(code)&&"交易不存在".equals(subMsg)){
                        System.out.println(oder1.getOderNo()+"订单未及时支付");
            }
            if ("10000".equals(code)&&"success".equals(msg)){
                System.out.println(oder1.getOderNo()+"已支付未更新");
            }
            //输出

        }

    }
}
